package com.ldzy.controller;

import com.ldzy.Exception.MyException;
import com.ldzy.pojo.Order;
import com.ldzy.pojo.Product;
import com.ldzy.vo.ProductVO;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.ldzy.pojo.User;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.IIOException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class MyFirstController {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public  void  hello(int id){
        System.out.println("Hello"+id);
    }
    @RequestMapping(value = {"/user/{name}/{sex}/{passwd}","/user"})
    public  void user(@PathVariable(value = "name",required = false) String name,@PathVariable(value = "sex") String sex,@PathVariable(value = "passwd") String passwd){
        System.out.println("姓名："+name+"性别："+sex+"密码："+passwd);
    }
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public  void  user(int id){
        System.out.println("Hello"+id);
    }
    @RequestMapping(value = "/user1")
    public  void user(@RequestParam(value = "name" ,required = false,defaultValue = "null") String name, String sex,int passwd){
        System.out.println("姓名："+name+"性别："+sex+"密码："+passwd);

    }
    @RequestMapping(value = "reginer")
    public  String reginer(){
        System.out.println("get reginer");
        return "reginer";
    }
    @RequestMapping(value = "/test1",method =RequestMethod.POST)
    public String test1(User user, Model model){
        System.out.println(user.toString());
        model.addAttribute("user",user);
        return  "reginer";
    }
    @RequestMapping("/test5")
    public  String test5(@DateTimeFormat(pattern = "yyyy-mm-dd") Date birdt){
        System.out.println(birdt);
        return "user";
    }
    @RequestMapping(value = "toaddProduct")
    public  String addProduct(){
        System.out.println("get addProduct");
        return "addProduct";
    }
    @RequestMapping("/test7")
    public  String test7(@RequestParam("product") String [] product){
        for (String proid:product){
            System.out.println(proid);
        }
        return "user";
    }
    @RequestMapping("/test8")
    public  String test8(@RequestParam("product") List<String> product){
        for (String proid:product){
            System.out.println(proid);
        }
        return "user";
    }
    @RequestMapping("/test9")
    public  String test9( ProductVO products){
        for (Product proid:products.getProducts()){
            System.out.println(proid);
        }
        return "user";
    }
    @RequestMapping("/toaddOrder")
    public  String addOrder(){
        return "addOrder";
    }
    @RequestMapping(value = "/test10",method = RequestMethod.POST)
    public  String test10( @RequestBody Order order){

            System.out.println(order.toString());

        return "user";
    }
    @RequestMapping("/test11")
    public  String test11( @RequestBody User user){

        System.out.print("11111111111111111111111");
        System.out.println(user.toString());

        return "user";
    }
    @RequestMapping(value = "/test12" ,method = RequestMethod.POST)
    public  String test12( @RequestBody List<User> users){

        for (User user:users)
            System.out.println(user.toString());
        return "user";
    }
    @RequestMapping(value = "/test13",method = RequestMethod.POST)
    public  String test13(@RequestBody List<User> users,@RequestBody List<Order> orders){
        for (User user:users)
            System.out.println(user.toString());
        for (Order order:orders)
            System.out.println(order.toString());
        return  "user";
    }
    @RequestMapping(value = "/test14",method = RequestMethod.GET)
    public ModelAndView test14(User user){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("reegin");
        return  modelAndView;
    }
    @RequestMapping(value = "/test15",method = RequestMethod.GET)
    public  void  test15(HttpServletResponse response){
        System.out.println("respinse");
        try {
            response.getWriter().write("respinse");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
@RequestMapping(value = "/test16")
@ResponseBody
    public  User  test16(User user){
        System.out.println(user.toString());
        return  user;
}
    @RequestMapping(value = "/test17")
    @ResponseBody
    public  List<User>  test17(@RequestBody User user){
        System.out.println(user.toString());
        List<User> userList=new ArrayList<User>();
        userList.add(user);
//        userList.add(user);
        return  userList;
    }
    @RequestMapping(value = "/test18",method =RequestMethod.GET )
    public  String test18(Double birdt) throws MyException{
        System.out.println(birdt);
        if (birdt==null) throw  new  MyException("您刚才提交的请求携带参数为"+birdt);
        return "user";
}
    @RequestMapping(value = "/test19",method = RequestMethod.POST)
    public  String test19( @RequestBody Order order)throws  MyException{

        System.out.println(order.toString());
        if(order==null) throw  new  MyException("网咯异常");
        return "user";
    }
}
